package com.yoomiito.app.utils

import cn.droidlover.xdroidmvp.cache.SharedPref
import com.alibaba.fastjson.JSONObject
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.yoomiito.app.Constant
import com.yoomiito.app.base.App
import com.yoomiito.app.model.bean.BannerAndIconInfo
import com.yoomiito.app.model.config.ShowTimeCache

object CacheUtils {
    //通知权限弹窗 只弹一次
    private const val PushTip = "show_push_tip_dialog"
    //友盟注册
    private const val UMRegister = "um_register"

    fun isFirstShowPushTipDialog() = SharedPref.getInstance(App.getInstance()).getBoolean(PushTip, false)

    fun savePushTipShoeRecord() {
        SharedPref.getInstance(App.getInstance()).putBoolean(PushTip, true)
    }

    fun isUMRegister() = SharedPref.getInstance(App.getInstance()).getBoolean(UMRegister, false)


    fun saveUMRegisterRecord() {
        SharedPref.getInstance(App.getInstance()).putBoolean(UMRegister, true)
    }

    fun removeUMRegisterRecord() {
        SharedPref.getInstance(App.getInstance()).putBoolean(UMRegister, false)
    }

    /**
     * 判断是否显示
     *  显示规则：每天一次  没有显示则返回true 否则返回false
     * 用于启动页广告和首页配置弹窗
     */
    fun showed(type: String, bannerAndIconInfo: BannerAndIconInfo): Boolean {
        val cacheDataStr = SharedPref.getInstance(App.getInstance()).getString(Const.Common.ShowTypeCache, "")
        val cacheData = (if (cacheDataStr.isNullOrEmpty()) {
            mutableMapOf<String, ShowTimeCache>()
        } else {
            val gson =  GsonBuilder().enableComplexMapKeySerialization().create()
            gson.fromJson<Map<String, ShowTimeCache>>(cacheDataStr,object : TypeToken<Map<String, ShowTimeCache>>() {}.type)
            //fastjson会报错 改用Gson
//            JSONObject.parseObject(cacheDataStr, object : TypeReference<Map<String, ShowTimeCache>>() {}) as Map<String, ShowTimeCache>
        }).toMutableMap()
        var cacheInfo = cacheData[type]
        if (cacheInfo == null) {
            cacheInfo = ShowTimeCache()
            cacheData[type] = cacheInfo
        }
        if (cacheInfo.id == bannerAndIconInfo.id && cacheInfo.time == DateUtils.currentDate()) return true
        cacheInfo.id = bannerAndIconInfo.id
        cacheInfo.time = DateUtils.currentDate()
        SharedPref.getInstance(App.getInstance()).putString(Const.Common.ShowTypeCache, JSONObject.toJSONString(cacheData))
        return false
    }

    fun saveTaoBaoSearchHistory(key:String){
        //跟淘宝搜索历史是一套，可以提出来统一处理
        var searchList = getTaoBaoSearchHistory()
        if (!searchList.contains(key)) {
            if (searchList.size >= 5) {
                for (i in 4 until searchList.size) {
                    searchList.removeAt(searchList.size - 1)
                }
            }
            searchList.add(0, key)
            SharedPref.getInstance(App.getInstance()).putListData(Constant.HISTORY, searchList)
        }
    }

    fun getTaoBaoSearchHistory(): MutableList<String> =  SharedPref.getInstance(App.getInstance()).getListData(Constant.HISTORY, String::class.java)?: mutableListOf()
}